{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tool-Augmented AgnoAgents Demo\n",
    "\n",
    "This notebook demonstrates how to use the tool-augmented agents in the SentientResearchAgent framework."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup\n",
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "# Add project root to path\n",
    "project_root = Path.cwd().parent\n",
    "sys.path.insert(0, str(project_root))\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()\n",
    "\n",
    "import os\n",
    "print(\"API Keys Available:\")\n",
    "print(f\"- OpenAI: {'✅' if os.getenv('OPENAI_API_KEY') else '❌'}\")\n",
    "print(f\"- Anthropic: {'✅' if os.getenv('ANTHROPIC_API_KEY') else '❌'}\")\n",
    "print(f\"- OpenRouter: {'✅' if os.getenv('OPENROUTER_API_KEY') else '❌'}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Direct AgnoAgent Usage with Tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from agno.agent import Agent\n",
    "from agno.models.openai import OpenAIChat\n",
    "from agno.tools.python import PythonTools\n",
    "from agno.tools.reasoning import ReasoningTools\n",
    "\n",
    "# Import our custom web_search tool\n",
    "from src.sentientresearchagent.hierarchical_agent_framework.tools.web_search_tool import web_search, clean_tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a simple agent with tools\n",
    "model = OpenAIChat(id=\"gpt-4o-mini\", temperature=0.2)\n",
    "\n",
    "# Prepare tools\n",
    "tools = [\n",
    "    PythonTools(base_dir=Path(\"tmp/python\"), run_code=True),\n",
    "    ReasoningTools(),\n",
    "    web_search\n",
    "]\n",
    "\n",
    "# Clean tools\n",
    "tools = clean_tools(tools)\n",
    "\n",
    "# Create agent\n",
    "agent = Agent(\n",
    "    model=model,\n",
    "    tools=tools,\n",
    "    system_message=\"\"\"You are a helpful research assistant with access to:\n",
    "- PythonTools: Execute Python code for calculations and analysis\n",
    "- ReasoningTools: Use structured reasoning\n",
    "- web_search: Search for current information\n",
    "\n",
    "Use these tools to provide accurate, well-researched answers.\"\"\",\n",
    "    show_tool_calls=True,\n",
    "    markdown=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test the agent\n",
    "response = agent.run(\"Calculate the compound interest on $10,000 at 5% annual rate for 10 years, compounded monthly\")\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test with web search\n",
    "response = agent.run(\"What is the current population of Tokyo?\")\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Using Tool-Augmented Agents in the Framework"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This would be used within the SentientResearchAgent framework\n",
    "# The agents are configured in agents.yaml with tools\n",
    "\n",
    "# Example configuration:\n",
    "config_example = \"\"\"\n",
    "- name: \"BasicReasoningExecutor\"\n",
    "  type: \"executor\"\n",
    "  adapter_class: \"ExecutorAdapter\"\n",
    "  description: \"Performs Analysis and Synthesis of information\"\n",
    "  model:\n",
    "    provider: \"openai\"\n",
    "    model_id: \"gpt-4o-mini\"  # OpenAI models work well with tools\n",
    "  agno_params:\n",
    "    reasoning: true\n",
    "  prompt_source: \"prompts.executor_prompts.REASONING_EXECUTOR_SYSTEM_MESSAGE\"\n",
    "  tools: [\"PythonTools\", \"ReasoningTools\"]  # Tools configured here!\n",
    "  registration:\n",
    "    action_keys:\n",
    "      - action_verb: \"execute\"\n",
    "        task_type: \"THINK\"\n",
    "    named_keys: [\"BasicReasoningExecutor\"]\n",
    "  enabled: true\n",
    "\"\"\"\n",
    "\n",
    "print(\"Agent Configuration Example:\")\n",
    "print(config_example)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Creating Custom Tool Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# You can create custom tool functions\n",
    "def analyze_sentiment(text: str) -> str:\n",
    "    \"\"\"Analyze the sentiment of the given text.\"\"\"\n",
    "    # Simple mock implementation\n",
    "    positive_words = ['good', 'great', 'excellent', 'happy', 'wonderful']\n",
    "    negative_words = ['bad', 'terrible', 'awful', 'sad', 'horrible']\n",
    "    \n",
    "    text_lower = text.lower()\n",
    "    positive_count = sum(1 for word in positive_words if word in text_lower)\n",
    "    negative_count = sum(1 for word in negative_words if word in text_lower)\n",
    "    \n",
    "    if positive_count > negative_count:\n",
    "        return \"Positive sentiment detected\"\n",
    "    elif negative_count > positive_count:\n",
    "        return \"Negative sentiment detected\"\n",
    "    else:\n",
    "        return \"Neutral sentiment detected\"\n",
    "\n",
    "# Add to agent\n",
    "tools_with_custom = tools + [analyze_sentiment]\n",
    "tools_with_custom = clean_tools(tools_with_custom)\n",
    "\n",
    "agent_with_sentiment = Agent(\n",
    "    model=model,\n",
    "    tools=tools_with_custom,\n",
    "    system_message=\"You are an assistant with sentiment analysis capabilities.\",\n",
    "    show_tool_calls=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test sentiment analysis\n",
    "response = agent_with_sentiment.run(\"Analyze the sentiment of: 'This is a wonderful day!'\")\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Key Takeaways\n",
    "\n",
    "1. **Tool Integration is Simple**: Just add tools to the `tools` list in agent configuration\n",
    "2. **Model Compatibility Matters**: OpenAI models work best with tools, some models may have issues\n",
    "3. **Custom Tools**: You can create any Python function as a tool\n",
    "4. **Clean Tools**: Always use `clean_tools()` to remove problematic attributes\n",
    "5. **Configuration**: In `agents.yaml`, add tools at the top level, not in `agno_params`"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}